package com.example.servlet;

import com.example.beans.UserBean;
import com.example.dao.UserDAO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/queryUser")
public class QueryServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String queryName = request.getParameter("queryName");
        UserDAO userDAO = new UserDAO();
        List<UserBean> users;
        if (queryName != null && !queryName.trim().isEmpty()) {
            users = userDAO.searchUsersByName(queryName);
        } else {
            users = userDAO.getAllUsers();
        }
        request.setAttribute("users", users);
        request.getRequestDispatcher("userinfo.jsp").forward(request, response);
    }
}
